#include "binswap.cpp"
#include  <stdlib.h>
#include <time.h>

const int maxtr = 14*27 ;
const int curlen = 28 ;
const int curdim = 14 ;
const int short1 = 4 ;
const int short2 = 7 ;
			int maxcount = 9 ;

int other_ok(CBinCode& B, CPermutation& a) {
	int i=0 ;
	for (; i<B.k &&
			!( B.row[i].is_set(a.tr[0]) ^ B.row[i].is_set(a.tr[1]) ) ;
			i++ ) ;
	return (i==B.k) ? 1 : 0 ;
} ;

int myrand (CBinCode& C, int trcnt, int arrtr[2][maxtr],
		CPermutation& a, CBinCode* X) {
	 if( trcnt==0 ) return 0 ;
	 randomize() ;
	 int x = random()%trcnt ;
	 a.tr[0] = arrtr[0][x] ;
	 a.tr[1] = arrtr[1][x] ;
	 cerr << "random :" << a.tr[0] << ' ' << a.tr[1] << endl ;
	 a.col_swap(C) ;
	 X[0] = C ;
	 return 1 ;
};

main (int argc, char *argv[]) {

		CBinCode *X ;
		CPermutation a(curlen) ;
		CBinWord tmp1(short1), tmp2(short2), r1(curlen), r2(curlen) ;
		char* no_arg_1="vayy" ;
		char* fname = (argv[1][0]) ? argv[1] : no_arg_1 ;
    filter (fname, X, 	NULL ) ;
		indep(X[2]) ;
		cerr << X[2] ;
		CBinCode B(X[2].k, X[2].n) ;
		B = X[2] ;

		int mycnt = 0 ;

		int arrtr[2][maxtr] ;
		int trcnt=0 ;

		X[0].convert() ;
		CBinCode C(curdim, curlen) ;
		C = X[0] ;
	do {
		trcnt=0 ;
		for ( int i1=0; i1<curlen; i1++ )	{
			for ( int j1=i1+1; j1<curlen; j1++ )	{
				a.tr[0] = i1 ;
				a.tr[1] = j1 ;
				if ( other_ok(B, a) ) {
					arrtr[0][trcnt] = i1 ;
					arrtr[1][trcnt] = j1 ;
					trcnt++ ;
					a.col_swap(C) ;
					cerr << "transp :  " << i1 << " " << j1 << endl ;
					// mycnt = 0 ;
					X[2].k = 0 ;
					for ( int i=1; i < (1 << tmp1.n); i++ )	{
						for ( int j=1; j < (1 << tmp2.n); j++ )	{

							tmp1.num = i ;
							tmp2.num = j ;
							r1 = tmp1^tmp2 ;
							r2 = tmp2^tmp1 ;

							if ( ! (C*r1).num ) {
								X[2].row[X[2].k] = r1 ;
								// mycnt++ ;
								X[2].k++ ;
								indep(X[2]) ;
								// mycnt = X[2].k ;
								// cout << r1 << endl;
								cerr << " = " << tmp1 << " ^ " << tmp2 << endl;
							}

							if ( ! (C*r2).num ) {
								X[2].row[X[2].k] = r2 ;
								// mycnt++ ;
								X[2].k++ ;
								indep(X[2]) ;
								// mycnt = X[2].k ;
								// cout << r2 << endl;
								cerr << " = " << tmp2 << " ^ " << tmp1 << endl;
							}
						}
					}
					B=X[2] ;
					// cout << '*' << endl ;
					cerr << "count :  " << X[2].k << endl ;
					mycnt = X[2].k  ;

					if ( kbhit() ) {
						maxcount = ( maxcount < mycnt ) ? mycnt : maxcount ;
						C.convert() ;
						cout 	<< "***it is last C :\n" << C
									<< "***max count is :\t" << maxcount << endl;
						cerr << "max count : " << maxcount << endl;
						exit(0) ;
					}

					if ( maxcount < mycnt ) {
						maxcount = mycnt ;
						CBinCode C1 = C ;
						C1.convert() ;
						C1.set_base(BIN) ;
						cout 	<< C1
									<< "***max count is :\t" << maxcount << "\n*"<< endl;
						cerr << "vect count : " << mycnt << endl;
						// C.convert() ;
						// exit(0) ;
					}
				}
				C = X[0] ;
			}
		}
	} while ( myrand(C, trcnt, arrtr, a, X) );

	return 0;
}
